home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / INTER39D.ZIP / INT2QH.ZIP / INT2QH2.C < prev    next >
C/C++ Source or Header  |  1991-04-18  |  6KB  |  299 lines

  1. /* INT2QH.C
  2.  *
  3.  * Author:   Kai Uwe Rommel
  4.  * Date:     Sun 07-Oct-1990
  5.  * Update:   Sat 20-Oct-1990
  6.  * Update:   Sun 11-Nov-1990 Ralf Brown
  7.  * Update:             Ralf Brown (added Bent Lynggaard's INT2GUID enh)
  8.  *
  9.  * Compiler: MS C 5.00 and newer / compact model, or TC 2.0 / compact model
  10.  * System:   PC/MS-DOS 3.20 and newer, OS/2 1.0 and newer
  11.  *
  12.  */
  13.  
  14. #define LABEL    "int2qh.c"
  15. #define VERSION  "1.3"
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. #define divider_line(s) (strncmp(s,"--------",8)==0)
  23.  
  24.  
  25. FILE *input, *output, *topics, *subtopics;
  26.  
  27. char line1[128];
  28. char line2[128];
  29. char category[128];
  30. char parent[128];
  31.  
  32. char infilename[14] = "interrup.lst";
  33. #define infileExt 9
  34. int splitInfile = 0;
  35.  
  36. int sub;
  37.  
  38.  
  39. void diskFull(void)
  40. {
  41.   fputs("\n\nDisk full\n", stderr);
  42.   fcloseall();
  43.  
  44.   unlink("topic.tmp");
  45.   unlink("subtopic.tmp");
  46.   exit(1);
  47. }
  48.  
  49. int _fputs(char *line, FILE *stream)
  50. {
  51.   char buffer[128];
  52.   int cnt = 0;
  53.  
  54.   while ( *line )
  55.   {
  56.     if ( *line == '\\' )
  57.       buffer[cnt++] = '\\';
  58.  
  59.     buffer[cnt++] = *line++;
  60.   }
  61.  
  62.   buffer[cnt] = 0;
  63.  
  64.   return fputs(buffer, stream);
  65. }
  66.  
  67. char *_fgets(char *s, int n, FILE *stream)
  68. {
  69.   char *ptr;
  70.   ptr = fgets(s, n, stream);
  71.   if ( (ptr==NULL) && (stream==input) && splitInfile )
  72.   {
  73.     fclose(input);
  74.     infilename[infileExt]++;
  75.     input = fopen(infilename, "r");
  76.     if ( input != NULL )
  77.     {
  78.       fprintf(stderr, "%s\n", infilename);
  79.       ptr = fgets(s, n, input);
  80.     }
  81.   }
  82.   return ptr;
  83. } /* _fgets */
  84.  
  85. void Initialize(void)
  86. {
  87.   input     = fopen(infilename, "r");
  88.   if ( input == NULL )
  89.   {
  90.     infilename[infileExt] = 'a';
  91.     infilename[infileExt+1] = 0;
  92.     input = fopen(infilename, "r");
  93.     if ( input == NULL )
  94.     {
  95.       fputs("Cannot open input file (INTERRUP.LST or INTERRUP.A)\n", stderr);
  96.       exit(1);
  97.     }
  98.     splitInfile = 1;
  99.   }
  100.   fprintf(stderr, "%s\n", infilename);
  101.   output    = stdout;
  102.   topics    = fopen("topic.tmp", "w");
  103.   subtopics = fopen("subtopic.tmp", "w");
  104.  
  105.   fprintf(topics,
  106.     ".context List Categories\n.list\nInterrupt-List\n"
  107.     ".context Interrupt-List\n.context INTLIST\n.topic Interrupt-List\n.list\n"
  108.     );
  109. }
  110.  
  111.  
  112. void Cleanup(void)
  113. {
  114.   fclose(topics);
  115.   fclose(subtopics);
  116.   fputs("Cleaning up\n", stderr);
  117.  
  118.   topics = fopen("topic.tmp", "r");
  119.   subtopics = fopen("subtopic.tmp", "r");
  120.  
  121.   while ( fgets(line1, sizeof(line1), topics) )
  122.     if ( fputs(line1, output) == EOF )
  123.       diskFull();
  124.  
  125.   while ( fgets(line1, sizeof(line1), subtopics) )
  126.     if ( fputs(line1, output) == EOF )
  127.       diskFull();
  128.  
  129.   fcloseall();
  130.  
  131.   unlink("topic.tmp");
  132.   unlink("subtopic.tmp");
  133. }
  134.  
  135.  
  136. void CopyFile(char *name)
  137. {
  138.   FILE *temp = fopen(name, "r");
  139.  
  140.   if ( temp == NULL )
  141.   {
  142.     fprintf(stderr, "WARNING: Could not open %s\n", name);
  143.     fputs("Information was not available\n", output);
  144.   }
  145.   else
  146.   {
  147.     while ( fgets(line2, sizeof(line2), temp) )
  148.       _fputs(line2, output);
  149.  
  150.     fclose(temp);
  151.   }
  152. }
  153.  
  154.  
  155. void StartTopic(char *name, char *desc)
  156. {
  157.   fprintf(sub ? subtopics : topics, "%s  %s\n", name, desc);
  158.   fprintf(output, ".context %s\n.category %s\n.topic %s\n", name, parent, desc);
  159. }
  160.  
  161.  
  162. void StartList(char *name, char *desc)
  163. {
  164.   fprintf(topics, "%s  %s (list)\n", name, desc);
  165.   fprintf(subtopics, ".context %s\n.category %s\n.topic %s\n.list\n", name, parent, desc);
  166.  
  167.   strcpy(category, desc);
  168.   strcpy(parent, name);
  169.   sub = 1;
  170. }
  171.  
  172.  
  173. void EndList(void)
  174. {
  175.   strcpy(category, "Interrupt-List");
  176.   strcpy(parent, "INTLIST");
  177.   sub = 0;
  178. }
  179.  
  180.  
  181. char *NextID(void)
  182. {
  183.   static char ID[32];
  184.   static unsigned long CurrentID = 0;
  185.  
  186.   CurrentID++;
  187.   sprintf(ID, "IL%04ld", CurrentID);
  188.  
  189.   return ID;
  190. }
  191.  
  192.  
  193. int RecognizedTopic(void)
  194. {
  195.   char *ptr, topic[64], desc[128];
  196.  
  197.   if ( _fgets(line2, sizeof(line2), input) == NULL )
  198.     return 0;
  199.  
  200.   if ( (ptr = strchr(line1, '-')) == NULL )
  201.     return 0;
  202.  
  203.   *--ptr = 0;
  204.   strcpy(topic, line1);
  205.   *ptr = ' ';
  206.  
  207.   if ( topic[strlen(topic) - 1] == 'h' )
  208.     topic[strlen(topic) - 1] = 0;
  209.  
  210.   if ( strcmp(category, topic) && sub )
  211.     EndList();
  212.  
  213.   strcpy(desc, line1);
  214.   desc[strlen(desc) - 1] = 0;
  215.  
  216.   for (ptr = line2 ; isspace(*ptr) ; ptr++)
  217.      ;
  218.  
  219.   if ( (!strncmp(ptr, "AX =", 4) ||
  220.     !strncmp(ptr, "AH =", 4) ||
  221.     !strncmp(ptr, "AL =", 4))
  222.        && !sub )
  223.     StartList(NextID(), topic);
  224.  
  225.   StartTopic(NextID(), desc);
  226.  
  227.   _fputs(line1, output);
  228.   if ( !divider_line(line2) )
  229.     _fputs(line2, output);
  230.  
  231.   return 1;
  232. }
  233.  
  234.  
  235. void CopyTopic(void)
  236. {
  237.   if ( divider_line(line2) )
  238.   { /* kludge for one-line interrupts */
  239.     _fgets(line1, sizeof(line2), input);
  240.     return;
  241.   }
  242.  
  243.   for (;;)
  244.   {
  245.     if ( _fgets(line1, sizeof(line1), input) == NULL )
  246.       break;
  247.  
  248.     if ( !divider_line(line1) )
  249.       _fputs(line1, output);
  250.     else
  251.     {
  252.       if ( _fgets(line2, sizeof(line2), input) == NULL )
  253.     break;
  254.  
  255.       if ( strncmp(line2, "INT ", 4) )
  256.       {
  257.     _fputs(line1, output);
  258.     _fputs(line2, output);
  259.       }
  260.       else
  261.       {
  262.     strcpy(line1, line2);
  263.     break;
  264.       }
  265.     }
  266.   }
  267. }
  268.  
  269.  
  270. void main(void)
  271. {
  272.   fprintf(stderr, "\nINT2QH %s - (c) Kai Uwe Rommel/Bent Lynggaard - %s\n", VERSION, __DATE__);
  273.  
  274.   Initialize();
  275.   EndList();
  276.  
  277.   StartTopic("HEADER", "Overview of the Interrupt List");
  278.   CopyTopic();
  279.  
  280.   StartTopic("MEMLIST", "BIOS Memory List");
  281.   CopyFile("memory.lst");
  282.  
  283.   StartTopic("Primer  ", "What is an interrupt?");
  284.   CopyFile("interrup.pri") ;
  285.  
  286.   StartTopic("INTERRUP.1ST","How to get/update the Interrupt List");
  287.   CopyFile("interrup.1st") ;
  288.  
  289.   while ( RecognizedTopic() )
  290.     CopyTopic();
  291.  
  292.   Cleanup();
  293.  
  294.   exit(0);
  295. }
  296.  
  297.  
  298. /* End of INT2QH.C */
  299.